<%@include file="init.jsp"%>
<%@include file="header.jsp"%>


	    <%

	Tree tree = new Tree<Key>();

    datastore = DatastoreServiceFactory.getDatastoreService();
    query = new Query("Project");
    projects = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(50));
	
	
	Key rootKey = KeyFactory.createKey("Project","root");
	Node<Key> root = new Node<Key>(rootKey);
	tree.setRoot(root);
	Node<Key> tmp;

		for (Entity project : projects) {
			if( project.getProperty("parentProject") == null ){
				tmp = new Node<Key>(project.getKey());
				root.addChild(tmp);
			}else{
				List<Node<Key>> list = tree.toList();
				Key parent = (Key) project.getProperty("parentProject");
				Node p = new Node((Key)project.getProperty("parentProject"));
				int index = list.indexOf(p);
				list.get(index).addChild(new Node(project.getKey()));
			} 
			//TODO handle kid first case
		}

	out.print(tree.toList());

		
	
  %>